package com.exporter.listeners;

import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import com.exporter.utility.DateComparator;
import com.exporter.utility.ExporterUtility;

public class AdvancedOptionsListener implements ItemListener
{
	JRadioButton radioExportFromDate;
	JComboBox comboExportFromDate;
	JRadioButton radioExportFilesOfDate;
	JComboBox comboFileOfDate;
	ButtonGroup radioBtnGroup;
	JTextField txtSourceFolder;
	JLabel lblUptoDate;
	JComboBox comboToDate;
	TitledBorder titleBorder;
	JTextArea txtarFolderIgnore;
	JTextArea txtarFileIgnore;
	JFrame frame;

	public AdvancedOptionsListener(JRadioButton radioExportFromDate, JComboBox comboExportFromDate, JLabel lblUptoDate, JComboBox comboToDate, JRadioButton radioExportFilesOfDate,
			JComboBox comboFileOfDate, ButtonGroup radioBtnGroup, JTextField txtSourceFolder, TitledBorder titleBorder, JTextArea txtarFolderIgnore, JTextArea txtarFileIgnore, JFrame frame)
	{
		this.radioExportFromDate = radioExportFromDate;
		this.comboExportFromDate = comboExportFromDate;
		this.radioExportFilesOfDate = radioExportFilesOfDate;
		this.comboFileOfDate = comboFileOfDate;
		this.radioBtnGroup = radioBtnGroup;
		this.txtSourceFolder = txtSourceFolder;
		this.lblUptoDate = lblUptoDate;
		this.comboToDate = comboToDate;
		this.titleBorder = titleBorder;
		this.txtarFolderIgnore = txtarFolderIgnore;
		this.txtarFileIgnore = txtarFileIgnore;
		this.frame = frame;
	}

	@Override
	public void itemStateChanged(ItemEvent ie)
	{
		if (ie.getStateChange() == ItemEvent.SELECTED)
		{
			titleBorder.setTitleColor(Color.black);
			SwingUtilities.updateComponentTreeUI(frame);
			radioExportFromDate.setEnabled(true);
			comboExportFromDate.setEnabled(true);
			lblUptoDate.setEnabled(true);
			comboToDate.setEnabled(true);
			radioExportFromDate.setSelected(true);
			radioExportFilesOfDate.setEnabled(true);
			comboFileOfDate.setEnabled(false);
			List<String> comboDates = ExporterUtility.analyzeAndGenerateDates(txtSourceFolder.getText(), txtarFolderIgnore.getText(), txtarFileIgnore.getText());
			String[] dateArr = new String[comboDates.size()];
			for (int i = 0; i < dateArr.length; i++)
			{
				dateArr[i] = comboDates.get(i);
			}
			ComboBoxModel comboFileOfDateModel = new DefaultComboBoxModel(dateArr);
			comboFileOfDate.setModel(comboFileOfDateModel);
			List<String> ascDates = new ArrayList<String>();
			ascDates.addAll(comboDates);
			Collections.sort(ascDates, new DateComparator("asc"));
			String[] ascDateArr = new String[ascDates.size()];
			for (int i = 0; i < ascDateArr.length; i++)
			{
				ascDateArr[i] = ascDates.get(i);
			}
			ComboBoxModel comboFileFromDateModel = new DefaultComboBoxModel(ascDateArr);
			ComboBoxModel comboFileToDateModel = new DefaultComboBoxModel(ascDateArr);
			comboExportFromDate.setModel(comboFileFromDateModel);
			comboToDate.setModel(comboFileToDateModel);
			titleBorder.setTitleColor(Color.black);
		}
		else
		{
			titleBorder.setTitleColor(Color.gray);
			SwingUtilities.updateComponentTreeUI(frame);
			radioBtnGroup.clearSelection();
			radioExportFromDate.setEnabled(false);
			comboExportFromDate.setEnabled(false);
			radioExportFilesOfDate.setEnabled(false);
			comboFileOfDate.setEnabled(false);
			lblUptoDate.setEnabled(false);
			comboToDate.setEnabled(false);
		}
	}

}
